package fhq.hcmute.edu.vn.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.ToggleButton;
import fhq.hcmute.edu.vn.activities.R;

/**
 * @author Hien Nguyen Oct 2, 2012
 */
public class MySettingAdapter extends ArrayAdapter<SettingItem> {

	private final List<SettingItem> list;
	private final Activity context;

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public MySettingAdapter(Context context, List<SettingItem> objects) {
		super(context, R.layout.listview_setting_item_title, objects);
		this.context = (Activity) context;
		this.list = objects;
	}

	public List<SettingItem> getList() {
		return list;
	}

	static class ViewHolder {
		private TextView txtSettingTitle;
		private TextView txtCaption;
		private TextView txtDescription;
		private ToggleButton toggleButton;
	}

	@Override
	public final boolean isEnabled(int position) {
		SettingItem setting = list.get(position);
		if (setting.hasSettingTitle()) {
			return false;
		}
		return true;
	}

	@Override
	public final View getView(final int position,
			View convertView, ViewGroup parent) {
		View row = convertView;
		SettingItem setting = list.get(position);
		if (convertView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			final ViewHolder viewHolder = new ViewHolder();
			if (setting.hasSettingTitle()) {
				row = inflater.inflate(R.layout.listview_title_item, null);
			} else {
				row = inflater.inflate(R.layout.listview_setting_item_title,
						null);
			}
			viewHolder.txtSettingTitle =
					(TextView) row.findViewById(R.id.lblSettingTile);
			viewHolder.txtCaption =
					(TextView) row.findViewById(R.id.lblCaption);
			viewHolder.txtDescription =
					(TextView) row.findViewById(R.id.lblDescription);
			viewHolder.toggleButton =
					(ToggleButton) row.findViewById(R.id.toggleButton);
			row.setTag(viewHolder);
		}
		ViewHolder holder = (ViewHolder) row.getTag();
		if (setting.hasSettingTitle()) {
			holder.txtSettingTitle.setVisibility(View.VISIBLE);
			holder.txtSettingTitle.setText(setting.getSettingTile());
		} else {
			if (setting.hasCheckBox()) {
				holder.toggleButton.setVisibility(View.VISIBLE);
				holder.toggleButton.setChecked(setting.isChecked());
			}
			holder.txtCaption.setText(setting.getCaption());
			holder.txtDescription.setText(setting.getDescription());
		}
		return row;
	}
}
